package com.cs252.flashcardz;

import java.util.List;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Opening extends Activity implements OnClickListener{
	public Button login, signUp;
	public EditText username, password;
	public String data = "", currUser = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_opening);
		
		login = (Button) findViewById(R.id.login);
		signUp = (Button) findViewById(R.id.signup);
		username = (EditText) findViewById(R.id.userET);
		password = (EditText) findViewById(R.id.passET);
		
		login.setOnClickListener(this);
		signUp.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.opening, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int check = 0;
		Thread userList;
		switch(v.getId()){
		//Login to database and get script to generate flash cards if any.
		case R.id.login:
			if((username.getText().length() > 0) && (password.getText().length() > 0)){
				AccessData ad= new AccessData();
				check = ad.login(username.getText().toString(), password.getText().toString());
				Log.i("check", "Check is equal to " + check);
				if(check == 0){
					Toast.makeText(this, "Incorrect Username or Password.", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(this, "Successful Login!", Toast.LENGTH_SHORT).show();
					//currUser = username.getText().toString();
					userList = new Thread(){
						public void run(){
							Intent openSubjectList = new Intent("com.cs252.flashcardz.SUBJECTLIST");
							startActivity(openSubjectList);
							
						}
					};
					userList.start();
				}
			}else{
				//Inform the user that no username or password was click.
                Toast.makeText(this, "Please enter a Username or Password with 1 or more characters.", Toast.LENGTH_SHORT).show(); 
			}
			break;
		//Create a new user
		case R.id.signup:
			if((username.getText().length() > 0) && (password.getText().length() > 0)){
				AccessData ad= new AccessData();
				check = ad.signUp(username.getText().toString(), password.getText().toString());
				
				if(check == 1){
					Toast.makeText(this, "Username already in use. Please try another.", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(this, "User created successfully!", Toast.LENGTH_SHORT).show();
					userList = new Thread(){
						public void run(){
							Intent openSubjectList = new Intent("com.cs252.flashcardz.SUBJECTLIST");
							startActivity(openSubjectList); 
							
						}
					};
				}
				
			}else{
				//Inform the user that no username or password was click.
                Toast.makeText(this, "Please enter a Username or Password with 1 or more characters.", Toast.LENGTH_SHORT).show(); 
			}
			break;
		default:
		}
		
	}
	
	public String getUser(){
		return currUser;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

}
